package com.example.lin.day9_12;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    private void call(String phone) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + phone));
        startActivity(intent);
    }

    private static final int REQUEST_CALL_PHONE_PERMISSION_CODE = 666;

    private void permissionCall() {
        //判断该权限是否已经获取
        int i = ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
        if (i != PackageManager.PERMISSION_GRANTED) {

            //判断该权限是否被 拒绝 过
            boolean b = ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE);
            Log.d(TAG, "permissionCall: " + b);

            if (b) {
                //被拒绝过,引导用户去 设置界面 打开该权限
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("权限申请")
                        .setMessage("微信需要打电话功能才能正常运行,请前往设置界面打开...")
                        .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", getPackageName(), null);
                        intent.setData(uri);
                        startActivity(intent);

                    }
                }).setNegativeButton("拒绝", null).show();
            } else {
                //没有被拒绝过,则表示第一次申请该权限.
                //权限申请(此时系统会弹出一个弹出框.  同意/拒绝)
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL_PHONE_PERMISSION_CODE);
            }
        } else {
            //已 授予权限
            call("10086");
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode) {
            case REQUEST_CALL_PHONE_PERMISSION_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //权限被授予
                    call("10086");
                } else {
                    //权限被拒绝
                    Toast.makeText(this, "权限被拒绝了...", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    public void test1(View view) {
        //在Android 6.0 版本及以上.需要动态获取打电话权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //申请打电话的权限
            permissionCall();
        } else {
            call("10086");
        }
    }
}
